﻿Imports System.Diagnostics
Imports System.Diagnostics.TraceSwitch


Public Class frmSQLTrace
    'Dim LastEventLogTime As Date
    Dim LastLogid As Integer = 0
    Private Sub frmSQLTrace_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Dim objLogs() As EventLog
            'objLogs = EventLog.GetEventLogs
            'For Each objLog In objLogs
            '    If objLog.LogDisplayName = "WHIS" Then
            '        objLog.Clear()
            '        Exit For
            '    End If
            'Next

            'Show_Curr_LogEntry()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            Me.DataGridView1.Refresh()

            'Me.ListView1.Clear()
            'Me.ListView1.Columns.Add("Date-Time")
            'Me.ListView1.Columns.Add("SQL Command")
            'Me.ListView1.Columns(0).Width = 156
            'Me.ListView1.Columns(1).Width = 263
            'Show_Last_LogEntry()
        Catch ex As Exception
            MsgBox(ex.Message)
            Debug.Print(ex.Message)
        End Try
    End Sub
    Public Sub Show_Curr_LogEntry(Optional ByVal LogName As String = "WHIS")
        Try
            'gets message for last entry in specified log
            'Dim strValue As String
            Dim objLogs() As EventLog
            Dim objLog As EventLog
            Dim str(5) As String
            Dim itm As ListViewItem
            Dim StrErr_Type As String = ""
            'Dim objEntry As EventLogEntry

            Dim Logid As Integer

            objLogs = EventLog.GetEventLogs()
            For Each objLog In objLogs
                If objLog.LogDisplayName = LogName Then
                    'objLog.Clear()
                    If Not objLog Is Nothing Then
                        LastLogid = objLog.Entries.Count - 1

                        For Logid = 0 To LastLogid
                            str(0) = objLog.Entries(Logid).TimeWritten
                            Select Case objLog.Entries(Logid).EntryType
                                Case 1
                                    StrErr_Type = "Error"
                                Case 2
                                Case 3
                                Case 4
                                    StrErr_Type = "Information"
                            End Select
                            str(1) = StrErr_Type

                            str(2) = objLog.Entries(Logid).Message
                            itm = New ListViewItem(str)
                            ListView1.Items.Add(itm)

                        Next
                    End If
                End If
            Next
            If objLog.Entries.Count = 0 Then
                LastLogid = 0
            Else
                LastLogid = objLog.Entries.Count - 1
            End If

        Catch ex As Exception

        End Try


    End Sub
    Public Sub Show_Last_LogEntry(Optional ByVal LogName As String = "WHIS")
        Try
            'gets message for last entry in specified log
            'Dim strValue As String
            Dim objLogs() As EventLog
            Dim str(5) As String
            Dim itm As ListViewItem
            'Dim objEntry As EventLogEntry
            Dim objLog As EventLog
            Dim Logid As Integer
            Dim ConLogID As Integer
            Dim StrErr_Type As String = ""
            objLogs = EventLog.GetEventLogs()
            For Each objLog In objLogs
                If objLog.LogDisplayName = LogName Then

                    If objLog.Entries.Count = 0 Then Exit Sub
                    If objLog.Entries.Count - 1 = LastLogid Then Exit Sub
                    ConLogID = LastLogid
                    LastLogid = objLog.Entries.Count - 1


                    For Logid = ConLogID To LastLogid
                        str(0) = objLog.Entries(Logid).TimeWritten
                        Select Case objLog.Entries(Logid).EntryType
                            Case 1
                                StrErr_Type = "Error"
                            Case 4
                                StrErr_Type = "Information"
                            Case Else
                                StrErr_Type = objLog.Entries(Logid).EntryType
                        End Select
                        str(1) = StrErr_Type
                        str(2) = objLog.Entries(Logid).Message
                        itm = New ListViewItem(str)
                        ListView1.Items.Insert(0, itm)


                    Next

                End If

            Next
            LastLogid = objLog.Entries.Count - 1
        Catch ex As Exception

        End Try


    End Sub

    Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Try
            If e.Button = Windows.Forms.MouseButtons.Right Then
                ContextMenuStrip1.Show(ListView1, e.Location)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub chkAlwayontheTop_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAlwayontheTop.CheckedChanged
        Select Case Me.chkAlwayontheTop.Checked
            Case True
                Me.TopMost = True
                Me.Show()
            Case False
                Me.TopMost = False
                Me.Show()
        End Select
    End Sub


    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub

    Private Sub BtnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub CopySQLCommandToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopySQLCommandToolStripMenuItem.Click
        Clipboard.Clear()
        Clipboard.SetText(Me.ListView1.Items(ListView1.SelectedIndices(0)).SubItems(2).Text)

    End Sub

    Private Sub BtnNotepad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNotepad.Click
        System.Diagnostics.Process.Start("notepad.exe")
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub ListView1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class